<template>
  <div class='dd-region-select'>
    <v-region-selects :town="isTown" v-model="regionSelectData" @change="regionChange" />
  </div>
</template>

<script>
export default {
  name: 'ddRegionSelect',
  props: {
    regionCode: {
        type: String,
        default: "440605"
    }
  },
  components: {},
  data () {
    return {
        isTown: false,
        regionSelectData: {
            province: "",
            city: "",
            area: "",
            town: "",
        }
    }
  },
  computed: {
   
  },
  mounted () {
    if(this.regionCode) {
        this.code2selector(this.regionCode);
        this.$emit("getRegion", this.regionSelectData)
    }
  },
  methods: {
    regionChange(val) {
        var regionData = this.selector2code(val);
        this.$emit("getRegion", regionData)
    },
    //数据库拿到的区域编码转换为选择器格式
    code2selector(regionCode) {
        this.regionSelectData = {
            province: "",
            city: "",
            area: "",
            town: "",
        };
        if (!regionCode) return;
        var province = regionCode.substring(0, 2).padEnd(6, "0");
        var city = regionCode.substring(0, 4).padEnd(6, "0");
        if (
            province == "120000" ||
            province == "110000" ||
            province == "310000" ||
            province == "500000" ||
            province == "810000" ||
            province == "820000"
        ) {
            city = province;
        }
        var area = regionCode.substring(0, 6);
        var town = regionCode.substring(0, 9);
        this.regionSelectData = {
            province,
            city,
            area,
            town: this.isTown ? town : "",
        };
    },
    //选择器格式数据转换为数据库需要存放的编码
    selector2code(selectedData) {
        var regionData = {
            regionCode: "",
            province: "",
            city: "",
            county: "",
            town: ""
        }
        if (selectedData.town) {
            regionData.regionCode = selectedData.town.key;
            regionData.province = selectedData.province.value;
            regionData.city = selectedData.city.value;
            regionData.county = selectedData.area.value;
            regionData.town = selectedData.town.value;
            return regionData;
        }
        if (selectedData.area) {
            if (selectedData.area.key.substring(4, 6) == "00") {
                regionData.regionCode = selectedData.area.key.substring(0, 4);
            } else {
                regionData.regionCode = selectedData.area.key.substring(0, 6);
            }
            regionData.regionCode = selectedData.area.key.substring(0, 6);
            regionData.province = selectedData.province.value;
            regionData.city = selectedData.city.value;
            regionData.county = selectedData.area.value;
            regionData.town = "";
            return regionData;
        }
        if (selectedData.city) {
            regionData.regionCode = selectedData.city.key.substring(0, 4);
            var regionCode = regionData.regionCode.substring(0, 2);
            if (
                regionCode == "12" ||
                regionCode == "11" ||
                regionCode == "31" ||
                regionCode == "50" ||
                regionCode == "81" ||
                regionCode == "82"
            ) {
                regionData.regionCode = selectedData.city.key.substring(0, 2) + "01";
            } else {
                regionData.regionCode = selectedData.city.key.substring(0, 4);
            }
            regionData.province = selectedData.province.value;
            regionData.city = selectedData.city.value;
            regionData.county = "";
            regionData.town = "";
            return regionData;
        }
        if (selectedData.province) {
            regionData.regionCode = selectedData.province.key.substring(0, 2);
            regionData.province = selectedData.province.value;
            regionData.city = "";
            regionData.county = "";
            regionData.town = "";
            return regionData;
        }
    },
  },
}
</script> 

<style lang='scss' scoped>
    .dd-region-select {
        position: relative;
    }
</style>
